<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\personnelLogic;
use Illuminate\Http\Request;

class personnelController extends Controller
{
    //添加新用户
    public function addStaff(Request $request)
    {
        try{
            $rules = [
                'username'=>'sometimes|string|max:20',
                'account'=>'required|string|max:20',
                'password'=>'required|string',
                'repeatPassword'=>'required|string',
                'position_id'=>'sometimes|numeric|exists:position,id',
            ];
            $this->verify($request->all(),$rules);
            $params = $request->all();
            if($params['password'] != $params['repeatPassword']) throw new \Exception('两次密码不一致,请检查');
            unset($params['repeatPassword']);
            $staff_id = (new personnelLogic())->addStaff($params);

            ret_json('ok','添加成功',['staff_id'=>$staff_id]);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取权限列表
    public function getAuthList(Request $request)
    {
        try{
            $list = (new personnelLogic())->getAuthList();
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取模块列表
    public function getModelList()
    {
        try{
            $list = (new personnelLogic())->getModelList();
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //编辑权限
    public function editAuth(Request $request)
    {
        try{
            $rules = [
                'id'=>'present|nullable|numeric',
                'authid'=>'present|nullable|array',
                'title'=>'required|string',
            ];

            $params = $request->all();
            $this->verify($params,$rules);
            $result = (new personnelLogic())->editAuth($params);
            ret_json('ok','编辑成功',$result);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //编辑模块
    public function editModel(Request $request)
    {
        try{
            $rules = [
                'id'=>'present|nullable|numeric',
                'title'=>'required|string',
                'icon'=>'required|string',
                'path'=>'required|string',
                'type'=>'required|numeric',
            ];

            $params = $request->all();
            $this->verify($params,$rules);
            $result = (new personnelLogic())->editModel($params);
            ret_json('ok','编辑成功',$result);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //修改用户信息
    public function editStaffMsg(Request $request)
    {
        try{
            $rules = [
                'uid'=>'sometimes|exists:staff,id|nullable',
                'account'=>'sometimes|string',
            ];
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            if(!is_null($params['roleid'])){
                $result = (new personnelLogic())->editStaffMsg($params);
            }else{
                $result = (new personnelLogic())->editStaffMsgBySelf($params);
            }
           
            ret_json('ok','编辑成功',$result);


        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //修改用户信息
    public function delStaff(Request $request)
    {
        try{
            $rules = [
                'uid'=>'required|exists:staff,id',
            ];
            $params = $request->all();
            $this->verify($params,$rules);
            $result = (new personnelLogic())->delStaff($params);
            ret_json('ok','编辑成功',$result);


        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //修改用户密码
   public function editPersonPwd(Request $request)
   {
    try{
        $rules = [
            'account'=>'required|exists:staff,account',
            'password'=>'required|string',
            'repeatPassword'=>'required|string',

        ];
        $params = $request->all();
        $this->verify($params,$rules);
        $result = (new personnelLogic())->editStaffMsgBySelf($params);
        ret_json('ok','编辑成功',$result);


    }catch (\Exception $e){
        ret_json('err',$e->getMessage());
    }
   }
}
